<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $flag
 * @property string $email
 * @property string $phone
 * @property string $money
 * @property string $address
 *
 * The followings are the available model relations:
 * @property Message[] $messages
 * @property Record[] $records
 * @property Replay[] $replays
 */
class User extends CActiveRecord
{
    public $passwordVerify;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('username', 'unique', 'message'=>'该用户名已被注册'),
			array('username', 'required','message'=>'用户名不能为空'),
			array('password', 'required','message'=>'密码不能为空'),
            //验证密码和确认密码
            array("passwordVerify","compare","compareAttribute"=>"password","message"=>"两次密码不一致"),
			array('username, password, flag, email, phone, money, address', 'length', 'max'=>45),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, username, password, flag, email, phone, money, address', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'messages' => array(self::HAS_MANY, 'Message', 'userid'),
			'records' => array(self::HAS_MANY, 'Record', 'userid'),
			'replays' => array(self::HAS_MANY, 'Replay', 'userid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => '用户名',
			'password' => '密码',
			'passwordVerify' => '确认密码',
			'flag' => 'Flag',
			'email' => '邮箱',
			'phone' => '电话',
			'money' => '余额',
			'address' => '地址',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('flag',$this->flag,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('money',$this->money,true);
		$criteria->compare('address',$this->address,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * 密码存储前md5加密
     */
    public function beforeSave() {
        $this->password=md5($this->password);
        return true;
    }

    /**
     * 删除记录之前的动作
     *
     */
    public function beforeDelete()
    {
        $user=User::model()->findByPk($this->id);
        if($user->username=='admin')
        {
            return false;
        }
        Message::model()->deleteAll('userid='.$this->id);
        Record::model()->deleteAll('userid='.$this->id);
        Replay::model()->deleteAll('userid='.$this->id);
        return true;
    }

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
